﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using EMC;
using System.IO;
using System.Reflection;

namespace Server
{

    public class Connection
    {
        private Socket listenSocket;
        private Socket filelistenSocket;
        private Socket chatListenSocket;
        //private String addressOfServer;
        //private String downloadTo;
        private bool running;
        private Dictionary<string, string> options;
        private ClientHandler protocolhandler;
        private IPAddress[] ipAddress;
        private Thread CommandListerner;
        private Thread ChatListener;
        private Thread FileListener;
        public static Dictionary<Socket, string> Clients;


        public Connection(Dictionary<string, string> Options)
        {
            options = Options;
            ipAddress = Dns.GetHostAddresses(Dns.GetHostName());
            CommandListerner = new Thread(startclient);
            ChatListener = new Thread(startchat);
            FileListener = new Thread(startfile);


        }

        public void stop()
        {
            running = false;
            CommandListerner.Abort();
            FileListener.Abort();
            ChatListener.Abort();

            //listenSocket.Poll(10, SelectMode.SelectRead);
            //listenSocket.Shutdown(SocketShutdown.Both);
            //listenSocket.Close();
            //filelistenSocket.Close();

        }

        public void start()
        {
            running = true;
            //            ListenForClients();
            CommandListerner.Start();
            FileListener.Start();
            ChatListener.Start();
        }


        private void startfile()
        {
            System.Diagnostics.Debug.WriteLine("File Socket Listening");

            filelistenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            filelistenSocket.Bind(new IPEndPoint(ipAddress[0], 5652));

            while (running == true)
            {
                filelistenSocket.Listen(100);

                //Socket fileSocket = filelistenSocket.Accept();
                //fileSocket.Listen(100);
                FileThread fileThread = new FileThread();
                //Files files = new Files(options);
                Socket newcon = filelistenSocket.Accept();

                //protocolhandler = new ClientHandler(options);

                Thread handelness = new Thread(new ParameterizedThreadStart(fileThread.fileHandle));
                handelness.Name = "Handle the FILES";
                handelness.Start(newcon);
            }

            //fileSocket.Close();
            //System.Diagnostics.Debug.WriteLine("END FILE SOCKET");
        }

        private void startclient()
        {
            listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listenSocket.Bind(new IPEndPoint(ipAddress[0], 5650));

            System.Diagnostics.Debug.WriteLine("Client Sokcet Listening");
            try
            {

                while (running == true)
                {

                    listenSocket.Listen(100);

                    Socket newcon = listenSocket.Accept();
                    protocolhandler = new ClientHandler(options);
                    Thread handelness = new Thread(new ParameterizedThreadStart(protocolhandler.handleProtocol));
                    handelness.Name = "Handle the Client";
                    handelness.Start(newcon);
                }

                listenSocket.Close();
                System.Diagnostics.Debug.WriteLine("END COMMAND SOCKET");
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err);
                listenSocket = null;
            }
        }
        private void startchat()
        {
            chatListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            chatListenSocket.Bind(new IPEndPoint(ipAddress[0], 5654));

            System.Diagnostics.Debug.WriteLine("Chat Socket Listening");
            try
            {

                while (running == true)
                {

                    chatListenSocket.Listen(100);

                    Socket newcon = chatListenSocket.Accept();
                    protocolhandler = new ClientHandler(options);
                    Thread handelness = new Thread(new ParameterizedThreadStart(protocolhandler.handleProtocol));
                    handelness.Name = "Handle the Chat";
                    handelness.Start(newcon);
                }

                chatListenSocket.Close();
                System.Diagnostics.Debug.WriteLine("END CHAT SOCKET");
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err);
                chatListenSocket = null;
            }
        }
        //private void sendMessage(Socket clientSocket, string protocolMessage)
        //{
        //    try
        //    {
        //        byte[] sendmess = Encoding.UTF8.GetBytes(protocolMessage);

        //        System.Diagnostics.Debug.WriteLine("Message sent: " + protocolMessage);

        //        clientSocket.Send(sendmess);
        //    }
        //    catch (Exception err)
        //    {
        //        System.Diagnostics.Debug.WriteLine(err);

        //    }

        //}

        //private string recMess(Socket incomming)
        //{
        //    byte[] buffer = new byte[1024];

        //    try
        //    {
        //        incomming.Receive(buffer);
        //        System.Diagnostics.Debug.WriteLine("Message Recived: " + Encoding.UTF8.GetString(buffer));
        //    }
        //    catch (Exception err)
        //    {
        //        System.Diagnostics.Debug.WriteLine(err);
        //    }
        //    return Encoding.UTF8.GetString(buffer);
        //}

        //private void ListenForClients()
        //{


        //    //Thread client = new Thread(startclient);
        //    //client.Start();



        //    //startclient();


        //    //while (running == true)
        //    //{
        //    //}

        //}

    }
}